package com.ywqf_api.service.impl;

import com.ywqf_api.dao.OrderDao;
import com.ywqf_api.entity.*;
import com.ywqf_api.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2018/4/2 0002.
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Override
    public FeeDetail orderDetail(Integer paymentId,Integer orderId,Integer type) {
        if(type == null || type == 0){            //type = 1水费，2电费，3采暖费，4车位费，5物业费
            return null;
        }
        if(type == 1){
            FeeDetail data = orderDao.findWaterDetail(paymentId);
            if(data.getPayType() == 1){
                if(data.getAddTime().length()>=19){
                    data.setAddTime(data.getAddTime().substring(0,19));
                }
                return data;
            }else{
                FeeDetail water = orderDao.orderWaterDetail(orderId);
                if(water != null){
                    if(water.getAddTime().length()>=19){
                        water.setAddTime(water.getAddTime().substring(0,19));
                    }
                }
                return water;
            }
        }else if(type == 2){
            FeeDetail data = orderDao.findElectDetail(paymentId);
            if(data.getPayType() == 1){
                if(data.getAddTime().length()>=19){
                    data.setAddTime(data.getAddTime().substring(0,19));
                }
                return data;
            }else{
                FeeDetail electricity = orderDao.orderElectricityDetail(orderId);
                if(electricity != null){
                    if(electricity.getAddTime().length()>=19){
                        electricity.setAddTime(electricity.getAddTime().substring(0,19));
                    }
                }
                return electricity;
            }
        }else if(type == 3){
            FeeDetail data = orderDao.findHeatingDetail(paymentId);
            if(data.getPayType() != 1 && data.getPayType() != 2){
                if(data.getAddTime().length()>=19){
                    data.setAddTime(data.getAddTime().substring(0,19));
                }
                return data;
            }else{
                FeeDetail heating = orderDao.orderHeatingDetail(orderId);
                if(heating != null){
                    if(heating.getAddTime().length()>=19){
                        heating.setAddTime(heating.getAddTime().substring(0,19));
                    }
                }
                return heating;
            }
        }else if (type == 4){
            FeeDetail data = orderDao.findParkingDetail(paymentId);
            if(data.getPayType() != 1 && data.getPayType() != 2){
                if(data.getAddTime().length()>=19){
                    data.setAddTime(data.getAddTime().substring(0,19));
                }
                return data;
            }else{
                FeeDetail parking = orderDao.orderParkingDetail(orderId);
                if(parking != null){
                    if(parking.getAddTime().length()>=19){
                        parking.setAddTime(parking.getAddTime().substring(0,19));
                    }
                }
                return parking;
            }
        }else{
            FeeDetail data = orderDao.findEstateDetail(paymentId);
            if(data.getPayType() != 1 && data.getPayType() != 2){
                if(data.getAddTime().length()>=19){
                    data.setAddTime(data.getAddTime().substring(0,19));
                }
                return data;
            }else{
                FeeDetail estate = orderDao.orderEstateDetail(orderId);
                if(estate!=null){
                    if(estate.getAddTime().length()>=19){
                        estate.setAddTime(estate.getAddTime().substring(0,19));
                    }
                }
                return estate;
            }
        }
    }

    @Override
    public order houseKeepOrderDetail(Integer orderId) {
        order houseKeep = orderDao.houseKeepOrderDetail(orderId);
        if(houseKeep != null){
            if(houseKeep.getOrderTime().length()>=19){
                houseKeep.setOrderTime(houseKeep.getOrderTime().substring(0,19));
            }
            if(houseKeep.getPayType() == 1){
                houseKeep.setPayTypeName("支付宝");
            }else if (houseKeep.getPayType() == 2){
                houseKeep.setPayTypeName("微信");
            }else{
                houseKeep.setPayTypeName("线下支付");
            }
        }
        return houseKeep;
    }

    @Override
    public List<order> findMyOrder(Integer customerId,Integer goodsTypeId) {
        List<order> findList = orderDao.findMyOrder(customerId,goodsTypeId);
        if(findList.size() > 0){
            for(int i=0;i<findList.size();i++){
                order oo = findList.get(i);
                if(oo.getOrderTime().length()>=19){
                    oo.setOrderTime(oo.getOrderTime().substring(0,19));
                }
            }
            return findList;
        }else{
            return null;
        }
    }

    @Override
    public List paymentRecords(Integer houseId, Integer type) {
        if(type == null || type == 0){            //type = 1水费，2电费，3采暖费，4车位费，5物业费
            return null;
        }
        if(type == 1){
            List<FeeList> list = orderDao.findWater(houseId);
            if(list.size()>0){
                for(int i=0; i<list.size();i++){
                    FeeList feeList = list.get(i);
                    if(feeList.getFeeTime().length() >=19){
                        feeList.setFeeTime(feeList.getFeeTime().substring(0,19));
                    }
                    Integer xtypes = feeList.getPayType();
                    if(xtypes == 0){
                        Integer types = orderDao.findOrderType(feeList.getOrderId());
                        if(types != null){
                            if(types == 1){
                                feeList.setPayTypeName("支付宝");
                            }else if (types == 2){
                                feeList.setPayTypeName("微信");
                            }else{
                                feeList.setPayTypeName("线下支付");
                            }
                        }
                    }else{
                        feeList.setPayTypeName("线下支付");
                    }
                }
                return list;
            }else{
                return null;
            }
        }else if(type == 2){
            List<FeeList> list = orderDao.findElect(houseId);
            if(list.size()>0){
                for(int i=0; i<list.size();i++) {
                    FeeList feeList = list.get(i);
                    if(feeList.getFeeTime().length() >=19){
                        feeList.setFeeTime(feeList.getFeeTime().substring(0,19));
                    }
                    Integer xtypes = feeList.getPayType();
                    if (xtypes == 0) {
                        Integer types = orderDao.findOrderType(feeList.getOrderId());
                        if (types != null) {
                            if (types == 1) {
                                feeList.setPayTypeName("支付宝");
                            } else if (types == 2) {
                                feeList.setPayTypeName("微信");
                            } else {
                                feeList.setPayTypeName("线下支付");
                            }
                        }
                    } else {
                        feeList.setPayTypeName("线下支付");
                    }
                }
                return list;
            }else{
                return null;
            }
        }else if(type == 3){
            List<FeeList> list = orderDao.findHeating(houseId);
            if(list.size()>0){
                for(int i=0; i<list.size();i++) {
                    FeeList feeList = list.get(i);
                    if(feeList.getFeeTime().length() >=19){
                        feeList.setFeeTime(feeList.getFeeTime().substring(0,19));
                    }
                    Integer xtypes = feeList.getPayType();
                    if (xtypes != 0) {
                        if (xtypes == 1) {
                            feeList.setPayTypeName("支付宝");
                        } else if (xtypes == 2) {
                            feeList.setPayTypeName("微信");
                        } else {
                            feeList.setPayTypeName("线下支付");
                        }
                    } else {
                        feeList.setPayTypeName("线下支付");
                    }
                    Integer goodsId = -3;
                    Integer orderId = orderDao.findOrderId(goodsId,feeList.getPaymentId());
                    feeList.setOrderId(orderId);
                }
                return list;
            }else{
                return null;
            }
        }else if(type == 4){
            List<FeeList> list = orderDao.findParking(houseId);
            if(list.size()>0){
                for(int i=0; i<list.size();i++) {
                    FeeList feeList = list.get(i);
                    if(feeList.getFeeTime().length() >=19){
                        feeList.setFeeTime(feeList.getFeeTime().substring(0,19));
                    }
                    if(feeList.getServiceStartDate().length() >= 19){
                        feeList.setServiceStartDate(feeList.getServiceStartDate().substring(0,19));
                    }
                    if(feeList.getServiceEndDate().length() >= 19){
                        feeList.setServiceEndDate(feeList.getServiceEndDate().substring(0,19));
                    }
                    Integer xtypes = feeList.getPayType();
                    if (xtypes != 0) {
                        if (xtypes == 1) {
                            feeList.setPayTypeName("支付宝");
                        } else if (xtypes == 2) {
                            feeList.setPayTypeName("微信");
                        } else {
                            feeList.setPayTypeName("线下支付");
                        }
                    } else {
                        feeList.setPayTypeName("线下支付");
                    }
                    Integer goodsId = -4;
                    Integer orderId = orderDao.findOrderId(goodsId,feeList.getPaymentId());
                    feeList.setOrderId(orderId);
                }
                return list;
            }else{
                return null;
            }
        }else{
            List<FeeList> list = orderDao.findEstate(houseId);
            if(list.size()>0){
                for(int i=0; i<list.size();i++) {
                    FeeList feeList = list.get(i);
                    if(feeList.getFeeTime().length() >=19){
                        feeList.setFeeTime(feeList.getFeeTime().substring(0,19));
                    }
                    if(feeList.getServiceStartDate().length() >= 19){
                        feeList.setServiceStartDate(feeList.getServiceStartDate().substring(0,19));
                    }
                    if(feeList.getServiceEndDate().length() >= 19){
                        feeList.setServiceEndDate(feeList.getServiceEndDate().substring(0,19));
                    }
                    Integer xtypes = feeList.getPayType();
                    if (xtypes != 0) {
                        if (xtypes == 1) {
                            feeList.setPayTypeName("支付宝");
                        } else if (xtypes == 2) {
                            feeList.setPayTypeName("微信");
                        } else {
                            feeList.setPayTypeName("线下支付");
                        }
                    } else {
                        feeList.setPayTypeName("线下支付");
                    }
                    Integer goodsId = -5;
                    Integer orderId = orderDao.findOrderId(goodsId,feeList.getPaymentId());
                    feeList.setOrderId(orderId);
                }
                return list;
            }else{
                return null;
            }
        }
    }
}
